<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>定时器示例</title>

</head>
<body>

<input type="text" id="text" value="0" >秒
<br>
<input type="button" value="开始计时" id="btn1">
<input type="button" value="停止计时" id="btn2">
<script type="text/javascript">
    var time = 0;
    var seconde=0;
    var btn1 = document.getElementById("btn1");
    var btn2 = document.getElementById("btn2");
    var text = document.getElementById("text");
    var count = 1;//设置时间戳
    btn1.addEventListener("click",showTime)
    function showTime() {
        if(count){
            time = setInterval(function () {
                seconde++;
                text.value = seconde;
            }, 1000)
            count =0;
        }
    }
    btn2.addEventListener("click",stopTime)
    function stopTime(){
        clearInterval(time);
        count =1;
    }
</script>
</body>
</html>